package com.jason.kotlin.gank.ui

import androidx.fragment.app.FragmentTransaction
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.ui.fragments.GanHuoTabFragment
import com.jason.kotlin.gank.ui.fragments.GankArticleTabFragment
import com.jason.kotlin.gank.ui.fragments.GankGirlFragment
import com.jason.kotlin.gank.ui.fragments.GankHomeFragment

class GankHomeDelegate(private var activity: GankHomeActivity) {

    private var fragment1 = GankHomeFragment.newInstance()
    private var fragment2 = GankGirlFragment.newInstance()
    private var fragment3 = GanHuoTabFragment.newInstance()
    private var fragment4 = GankArticleTabFragment.newInstance()

    private var currentPosition = 0

    fun getOwnerActivity() = activity

    fun setMenuSelection(flag: Int, selectTitle: String) {
        activity.title = selectTitle
        // 开启一个Fragment事务
        val fragmentTransaction: FragmentTransaction =
            activity.supportFragmentManager.beginTransaction()
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(fragmentTransaction)
        when (flag) {
            R.id.nav_home -> {
                currentPosition = 0
                fragmentTransaction.replace(R.id.frame_content, fragment1)
                fragmentTransaction.show(fragment1)
            }
            R.id.nav_girl -> {
                currentPosition = 1
                fragmentTransaction.replace(R.id.frame_content, fragment2)
                fragmentTransaction.show(fragment2)
            }
            R.id.nav_category_ganhuo -> {
                currentPosition = 2
                //fragmentTransaction.show(fragment3)
                fragmentTransaction.replace(R.id.frame_content, fragment3)
                fragmentTransaction.show(fragment3)
            }
            R.id.nav_category_article -> {
                currentPosition = 3
                //fragmentTransaction.show(fragment3)
                fragmentTransaction.replace(R.id.frame_content, fragment4)
                fragmentTransaction.show(fragment4)
            }
        }
        fragmentTransaction.commit()
    }

    private fun hideFragments(transaction: FragmentTransaction) {
        transaction.hide(fragment1)
        transaction.hide(fragment2)
        transaction.hide(fragment3)
        transaction.hide(fragment4)
    }
}